const app = getApp()
const WXAPI = require('../../utils/main');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loginstate: "0",
     phoneNumber: "",
     userInfo:{},
    showModal: false,//定义登录弹窗
    yonghu: false,//用户
    hasUserInfo:false,
  },
  //在页面加载的时候，判断缓存中是否有内容，如果有，存入到对应的字段里
  onLoad: function () {
    var that = this;
    that.onShow()
  },
  onShow:function () {
    var that = this;
    app.globalData.phoneNumber = wx.getStorageSync('phoneNumber');
    app.globalData.userInfo = wx.getStorageSync('userInfo');
    if(wx.getStorageSync('userInfo') != ''){
      that.setData({
        phoneNumber: wx.getStorageSync('phoneNumber'),
        userInfo:wx.getStorageSync('userInfo'),
        hasUserInfo: true
      })
    }else{
      that.setData({
        phoneNumber: '',
        userInfo:'',
        hasUserInfo: false
      })
    }
  },
  //收货地址
  address:function(){
    wx.navigateTo({
      url:'../address/address'
    })
  },
  //用户登录  
  onGotUserInfo: function (e) {
    wx.navigateTo({
      url:'./login'
    })
  },
  // onGotUserInfo: function (e) {
  //   console.log(e)
  //   var that = this;
  //   let cloudID = e.detail.cloudID
  //   that.setData({
  //     cloudID:cloudID
  //   })
  //   wx.cloud.callFunction({
  //     name: 'login',
  //     complete: res => {
  //     console.log(res)
  //      console.log('云函数获取到的openid: ', res.result.openid)
  //      var openid = res.result.openid;
  //      that.setData({
  //       openid: openid
  //      })
  //     }
  //    })
  //   if (e.detail.errMsg == "getUserInfo:ok") {
  //     wx.setStorageSync('userInfo', e.detail.userInfo)
  //     console.log('进入')
  //     that.setData({ 
  //       userInfo: e.detail.userInfo,
  //       hasUserInfo:true
  //     });
  //     wx.navigateTo({
  //       url:'./login'
  //     })
  //     // that.showDialogBtn();//调用一键获取手机号弹窗（自己写的）
  //   }else{
  //     that.shouquan()
  //   }
  // },
 
  // 显示一键获取手机号弹窗
  showDialogBtn: function () {
    this.setData({
      showModal: true//修改弹窗状态为true，即显示
    })
  },
  // 隐藏一键获取手机号弹窗
  hideModal: function () {
    this.setData({
      showModal: false//修改弹窗状态为false,即隐藏
    });
  },

  //绑定手机
  getPhoneNumber: function (e) {
    console.log(e)
    var that = this;
    that.hideModal();
    wx.checkSession({
      success: function () {
        wx.cloud.callFunction({
          name: 'phone',
          data: {
            action:'getcellphone',
            id:e.detail.cloudID
          }
        }).then(res => {
          console.log('res: ', res)
        
        })    
      },
    })
  },

  shouquan:function(){
    //用户按了拒绝按钮
    wx.showModal({
        title: "警告",
        content: "您点击了拒绝授权，将无法使用小程序，请授权之后再使用!!!",
        showCancel: false,
        confirmText: "返回授权",
        success: function(res) {
            // 用户没有授权成功，不需要改变 isHide 的值
            if (res.confirm) {
                console.log("用户点击了“返回授权”");
            }
        },
    });
  },
  // 订单按钮
  myorderBtn() {
      if (wx.getStorageSync("userInfo")) {
          wx.navigateTo({
              url: "../my-order/my-order",
          });
      } else {
          wx.showToast({
              title: "请先登录",
              icon: "none",
          });
      }
  },
  //待付款
  obligationBtn() {
      if (wx.getStorageSync("userInfo")) {
          wx.navigateTo({
              url: "../my-order/my-order?type=0",
          });
      } else {
          wx.showToast({
              title: "请先登录",
              icon: "none",
          });
      }
  },
  // 待发货
  sendGoodsBtn() {
      if (wx.getStorageSync("userInfo")) {
          wx.navigateTo({
              url: "../my-order/my-order?type=1",
          });
      } else {
          wx.showToast({
              title: "请先登录",
              icon: "none",
          });
      }
  },
  // 已发货
  waitReceivingBtn() {
      if (wx.getStorageSync("userInfo")) {
          wx.navigateTo({
              url: "../my-order/my-order?type=2",
          });
      } else {
          wx.showToast({
              title: "请先登录",
              icon: "none",
          });
      }
  },
  // 已完成
  Completed:function(){
    if (wx.getStorageSync("userInfo")) {
      wx.navigateTo({
          url: "../my-order/my-order?type=3",
      });
    }else {
      wx.showToast({
          title: "请先登录",
          icon: "none",
      });
    }
  },
  // 用户中心
  user_center:function(){
    wx.navigateTo({
        url: "../user/user",
    });
  },
  // 账户设置
  Account:function(){
    wx.navigateTo({
        url: "../set_up/set_up",
    });
  },
  //退换/售后
  after_sale:function(){
    wx.navigateTo({
      url: "../after-sale/after-sale",
    });
  }
})
